package jaux.tank.service.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jaux.tank.bean.constant.cache.Cache;
import jaux.tank.bean.constant.cache.CacheKey;
import jaux.tank.bean.entity.system.Cfg;
import jaux.tank.dao.system.CfgRepository;
import jaux.tank.utils.PageUtils;
import jaux.tank.utils.StringUtils;
import jaux.tank.utils.factory.Page;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * CfgService
 *
 * @author zt
 * @version 2018/11/17 0017
 */

@Service
@Transactional
public class CfgService {
	@Autowired
	CacheManager cacheManager;
    @Autowired
    private CfgRepository cfgRepository;
    public Page findPage(Page<Cfg> page ,final Map<String,String> params) {
    	Pageable pageable = PageUtils.convertPageToPageable(page);

        org.springframework.data.domain.Page<Cfg> pageResult = cfgRepository.findAll(new Specification<Cfg>(){


            @Override
            public Predicate toPredicate(Root<Cfg> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtils.isNotEmpty(params.get("cfgName"))){
                    list.add(criteriaBuilder.like(root.get("cfgName").as(String.class),"%"+params.get("cfgName")+"%"));
                }

                if(StringUtils.isNotEmpty(params.get("cfgValue"))){
                    list.add(criteriaBuilder.like(root.get("cfgValue").as(String.class),"%"+params.get("cfgValue")
                            +"%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        },pageable);
        page.setTotal(Integer.valueOf(pageResult.getTotalElements()+""));
        page.setRecords(pageResult.getContent());
        return page;
    }

    @CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.CFG + "'+#cfg.cfgName")
    public void save(Cfg cfg) {
        cfgRepository.save(cfg);
    }

    public void delete(String id) {
    	Optional<Cfg> cfgOpt = cfgRepository.findById(id);
    	if(cfgOpt.isPresent()) {
    		org.springframework.cache.Cache cache = cacheManager.getCache(Cache.CONSTANT);
    		if(cache !=null) {
    			cache.evict("'" + CacheKey.CFG + "'+"+cfgOpt.get().getCfgName());
    		}
    		cfgRepository.deleteById(id);
    	}
    }
}
